// Package user
/*
@Coding : utf-8
@Time : 2023/3/11 21:38
@Author : yizhigopher
@Software : GoLand
*/
package user

import (
	"ginchat/internal/globals/codes"
	"ginchat/internal/globals/snowflake"
	"ginchat/internal/services"
	"ginchat/internal/vo"
	"github.com/gin-gonic/gin"
)

func LoginHandler(ctx *gin.Context) {

}

func RegisterHandler(ctx *gin.Context) {
	var req vo.UserRegister
	if err := ctx.ShouldBind(&req); err != nil {
		ctx.JSON(200, gin.H{
			"code":    codes.ParameterIllegal,
			"message": "get parameter failed",
			"error":   err.Error(),
		})
		return
	}

	id := snowflake.MakeID()

	var user = &services.UserService{}

	user.Assign(req)
	user.UserId = id

	if err := user.Add(); err != nil {
		ctx.JSON(200, gin.H{
			"code":    codes.DBError,
			"message": "add user information failed",
			"error":   err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    codes.OK,
		"message": "add successfully",
		"data":    user,
	})
	return
}
